import urllib.request
import urllib.parse

url = 'https://fanyi.baidu.com/sug'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0'
}

data = {
    'kw': 'spider'
}

# post 请求的参数，必须编码
data = urllib.parse.urlencode(data).encode('utf-8')

# psot 的请求参数 是不会拼接在url的后面，需要放在请求对象的定制参数当中 post的请求参数必须进行编码，类型
request = urllib.request.Request(url=url, data=data, headers=headers)

# 模拟浏览器服务器发送请求
# 发起请求并处理响应
try:
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    print(content)
except urllib.error.URLError as e:
    print(f"请求失败: {e}")
except Exception as e:
    print(f"发生未知错误: {e}")

import json
obj = json.loads(content)
print(obj)